package co.yixiang.yshop.module.express.controller.admin.electronicsorder;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.express.controller.admin.electronicsorder.vo.*;
import co.yixiang.yshop.module.express.dal.dataobject.electronicsorder.ElectronicsOrderDO;
import co.yixiang.yshop.module.express.convert.electronicsorder.ElectronicsOrderConvert;
import co.yixiang.yshop.module.express.service.electronicsorder.ElectronicsOrderService;

@Tag(name = "管理后台 - 电子面单")
@RestController
@RequestMapping("/express/electronics-order")
@Validated
public class ElectronicsOrderController {

    @Resource
    private ElectronicsOrderService electronicsOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建电子面单")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:create')")
    public CommonResult<Long> createElectronicsOrder(@Valid @RequestBody ElectronicsOrderCreateReqVO createReqVO) {
        return success(electronicsOrderService.createElectronicsOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电子面单")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:update')")
    public CommonResult<Boolean> updateElectronicsOrder(@Valid @RequestBody ElectronicsOrderUpdateReqVO updateReqVO) {
        electronicsOrderService.updateElectronicsOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电子面单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('express:electronics-order:delete')")
    public CommonResult<Boolean> deleteElectronicsOrder(@RequestParam("id") Long id) {
        electronicsOrderService.deleteElectronicsOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电子面单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<ElectronicsOrderRespVO> getElectronicsOrder(@RequestParam("id") Long id) {
        ElectronicsOrderDO electronicsOrder = electronicsOrderService.getElectronicsOrder(id);
        return success(ElectronicsOrderConvert.INSTANCE.convert(electronicsOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得电子面单列表")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<List<ElectronicsOrderRespVO>> getElectronicsOrderList() {
        List<ElectronicsOrderDO> list = electronicsOrderService.getElectronicsOrderList();
        return success(ElectronicsOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电子面单分页")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<PageResult<ElectronicsOrderRespVO>> getElectronicsOrderPage(@Valid ElectronicsOrderPageReqVO pageVO) {
        PageResult<ElectronicsOrderDO> pageResult = electronicsOrderService.getElectronicsOrderPage(pageVO);
        return success(ElectronicsOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/changeStatus")
    @Operation(summary = "设置主配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> changeStatus(@RequestParam("id") Long id,@RequestParam("isMain") Integer isMain) {
        electronicsOrderService.changeStatus(id,isMain);
        return success(true);
    }

}
